import pyglet
from windoh import *
import cairo
import math
import array
import ctypes


if False:
    from windoh.cairo_renderer import CairoStyle, CairoRenderer
    styleclass = CairoStyle
    renderclass = CairoRenderer
else:
    from windoh.opengl_renderer import OpenGLStyle, OpenGLRenderer
    styleclass = OpenGLStyle
    renderclass = OpenGLRenderer

style = styleclass(initial={
    'Button.delete': {'background_color': (0.5, 0.5, 0.8)},
    '#exit': {'background_color' : (0.8, 0.5, 0.5)},
    '.small': {'font_size': 8},
})

print style

but1 = Button(label="but1", width=50, height=20, x=10, y=10)
print style.getForControl(but1)

but2 = Button(label="but2", classname="delete", width=70, height=20, x=70, y=10)
print style.getForControl(but2)

but3 = Button(label="but3", name="exit", width=60, height=20, x=150, y=10)
print style.getForControl(but3)

but4 = Button(label="but4", classname="small", width=150, height=20, x=220, y=10)
print style.getForControl(but4)

frame1 = Frame(label="hello", width=200, height=200, x=100, y=100)
window1 = Window(label="hello", width=200, height=200, x=400, y=100)

win_but = Button(label="another button", width=120, height=20, x=50, y=50)
window1.append(win_but)

@win_but.event
def on_mouse_press(x, y, b, m):
    print "win_but", x, y, b, m
    return True

controls = [but1, but2, but3, but4, frame1, window1]

renderer = renderclass(style)

print renderer

import pyglet
from pyglet.gl import *

window = pyglet.window.Window()
root = RootWindow(window, renderer, children=controls)

@window.event
def on_close():
    pyglet.app.exit()

@window.event
def on_draw():
    window.clear()
    #renderer.render(controls)
    root.render()

@but4.event
def on_mouse_press(x, y, b, m):
    print "but4!", x, y, b, m
    s = but4.style
    if not s.has_key('font_size') or s['font_size'] != 14:
        s['font_size'] = 14
    else:
        s['font_size'] = 8
    del but4.computed_style
    but4.dirty = True
    return True

@but3.event
def on_mouse_motion(x, y, dx, dy):
    print "motion"

@but2.event
def on_mouse_release(x, y, b, m):
    print "release"

def runfunc():
    pyglet.app.run()

if False:
    import os, sys
    import hotshot
    curdir = os.getcwd()
    prof_file = os.path.join(curdir, sys.argv[0] + '.prof')
    prof = hotshot.Profile(prof_file)
    prof.runcall(runfunc)
    print "closing profile"
    prof.close()

    import hotshot.stats
    print "loading profile..."
    stats = hotshot.stats.load(prof_file)
    print "sorting stats..."
    stats.sort_stats("time").print_stats()
else:
    runfunc()




